'use strict';
import React from "react";
import {View, StyleSheet, Text, TouchableOpacityComponent, TouchableOpacity} from "react-native";
import ImageView from "../common/ImageView";

class FreeForumComponent extends React.PureComponent {

    constructor(props) {
        super(props);
    }

    _onPressDZ = async () => {
        const {onPressDZ} = this.props;
        const {item,index} = this.props;
        let give_like = (item.give_like && item.give_like >= 0) ? parseInt(item.give_like) : 0;
        const result = await Services.post(ServicesApi.FORUM_DZ,{topic_id:item.topic_id,create_user:global.secretKey});
        if (result.code === StatusCode.SUCCESS_CODE) {
            console.log('item.give_like---->', item.give_like);
            console.log('give_like---->', give_like);
            give_like = give_like + 1;
            item.give_like = give_like;
            item.like_state = 1;
            const newItem = {...item};
            // console.log('item--------->',newItem);
            onPressDZ && onPressDZ(item,index);
        }else {
            ToastManager.show(result.dec || 'error');
        }
    };

    _onPressComment =  () => {
        const {onPressComment} = this.props;
        const {item,index} = this.props;
        RouterHelper.navigate('评论','CommentList',{topic_id:item.topic_id,reply_module:'topic'});
    };

    _onPressItem = () => {
        const {item,index} = this.props;
        RouterHelper.navigate(item.topic_title,'TopicDetail',{topic_id:item.topic_id});
    };

    render() {
        const {item,index} = this.props;
        return (
            <TouchableOpacity
                style={styles.itemView}
                onPress={this._onPressItem}>
                <View style={styles.itemHeadView}>
                    <Text style={styles.itemTitle} numberOfLines={1}>{item.topic_title}</Text>
                    <Text style={styles.itemTime}>{Moment(item.create_time).format('MM月DD日 HH:mm')}</Text>
                </View>
                <Text style={styles.itemContent} numberOfLines={2}>{item.topic_content}</Text>
                <View style={styles.itemBottomView}>
                    <View style={styles.itemLeftBottomView}>
                        <ImageView
                            style={styles.itemUserHead}
                            resizeMode={'cover'}
                            source={item.user_img ? {uri: item.user_img} : Images.icon_default_avatar}
                        />
                        <Text style={styles.itemUserName}>{item.user_nickname}</Text>
                    </View>
                    <View style={styles.itemRightBottomView}>
                        <TouchableOpacity
                            style={[Theme.RCC]}
                            onPress={this._onPressDZ}
                        >
                            <ImageView
                                tintColor={ item.like_state == 1 ? null : '#333'}
                                style={[styles.icon]}
                                source={Images.icon_bull}
                            />
                            <Text style={styles.textCount}>{item.give_like || 0}</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={{flexDirection: 'row',alignItems:'center'}} onPress={this._onPressComment}>
                            <ImageView
                                style={[styles.icon,{marginLeft: 12}]}
                                source={Images.icon_comment_msg}
                            />
                            <Text style={styles.textCount}>{item.comments_count}</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    itemView: {
        marginTop: 13,
        marginHorizontal: 15,
        borderRadius: 4,
        borderWidth: Theme.minPixel,
        borderColor: "#dbdbdb",
        padding: 8,
    },
    itemHeadView: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center',
    },
    itemTitle: {
        fontSize: FontSize(16),
        color: "#333",
        flex: 1,
    },
    itemTime: {
        fontSize: FontSize(12),
        color: "#9e9c99",
        marginLeft: 5,
    },
    itemContent: {
        fontSize: FontSize(13),
        color: "#a8a8a8",
        marginTop: 5,

    },
    itemBottomView: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center',
        marginTop: 10,
    },
    itemLeftBottomView: {
        flexDirection: 'row',
        alignItems: 'center',
    },
    itemUserHead: {
        width: 16,
        height: 16,
        borderRadius: 16/2,
    },
    itemUserName: {
        marginLeft: 5,
        fontSize: FontSize(13),
        color: "#707070",
    },
    itemRightBottomView: {
        flexDirection: 'row',
        alignItems: 'center',
    },
    icon: {
        width:14,
        height: 14,
    },
    iconGray: {
    },
    textCount: {
        fontSize: FontSize(13),
        color: "#707070",
        marginLeft: 3,
    },
});

export default FreeForumComponent